/**
 * Created by chenyan on 2018/6/24.
 */
var oamContentId = GetQueryString('contentId') ? GetQueryString('contentId') : 0; //短视频标题id
var oamkeywords = GetQueryString('keywords') ? GetQueryString('keywords') : 0; //关键词

//js-全局变量 短视频详情信息
var oamDetailVideoUrl=''; //短视频url
var oamDetailVideoPoster='';//短视频Poster
var oamDetailVideoTitle='';//短视频标题
var oamDetailVideoTotalURL='';//跳转长视频的url
var oamDetailVideoDuration='';//短视频的时长
var oamDetailVideoIntro='';//短视频下的简介
var videoPlayer = null; //判断是否存在video
var programCibnId='';//长视频参数

//微信分享-数据全局变量
window.shareData = {
		"url": encodeURI(window.location.href),
		"title":"",
		"description": "",
		"iconUrl": ""
	};
    
//初始化详情页   
$(function () {
    /*完成video部分的加载*/
    getOAMProgramDetailAjax();
    /*完成精彩导视部分的加载*/
    getOAMRecommendListAjax()
})

/*********************************video部分相关函数开始************************/
/*请求详情data*/
function getOAMProgramDetailAjax() {
    var keyData = {contentId:oamContentId}; //短视频ID
    var basePar = initPar();
    $.extend(keyData, basePar);
    $.ajax({
        url:website + "wxcibn/getShortVideoDetail", //获取短视频详情接口
        type: "get",
        async: true,
        dataType: 'json',
        data: keyData,
        beforeSend: function () {
        },
        success: function (returnData) {
            data = returnData.data;
            if (returnData.statusCode === 0) {
                if(data.contentId){
                    //返回成功，设置短视频详情页数据
                    oamDetailVideoUrl = formatFileUrl(data.programUrl,data.programUrl,1);
                    oamDetailVideoPoster = formatFileUrlImgCut(data.contentImg,data.contentImg,1,1);
                    oamDetailVideoTitle = data.title;
                    programCibnId = data.programCibnId;
                    oamDetailVideoDuration = initTimeLength(data.endTime - data.startTime);
                    oamDetailVideoIntro = data.description;
                    
                    //设置微信分享-参数
                    window.shareData.title = data.title;
                    window.shareData.description = data.description;
                    window.shareData.iconUrl = oamDetailVideoPoster;
                    appendHtml();
                }
            }
        },
    })
};

/*填充详情视频相关模块html*/
function appendHtml() {
    var html =`
        <section class="oam-video_item">
            <div class="oam-item">
                <div class="oam-video-container " id="video_container" data-video-url=${oamDetailVideoUrl} data-video-poster=${oamDetailVideoPoster}></div>
                <img src=${oamDetailVideoPoster} alt=""
                     class="oam-video-poster">
                <div class="oam-video-play" onclick="videoInit()">
                    <img src="img/play.png" alt="">
                </div>
                <span class="oam-video-duration">${oamDetailVideoDuration}</span>
            </div>
            <div class="oam-item-bottom clearfix">
                <p class="oam-bottom-left">${oamDetailVideoTitle}</p>
                <div class="oam-bottom-right">
                    <div class="oam-full-film" onclick="skipFull()">
                        <img src="img/playFullDetail.png" alt="">
                        <span>看全片</span>
                    </div>
                    <div class="oam-share" onclick="detailShare()">
                        <img src="img/shareDetail.png" alt="">
                        <span>分享</span>
                    </div>
                </div>
            </div>
        </section>
        <div class="oam-intro">${oamDetailVideoIntro}</div>
        `
    $(".oam-detailV").append(html);
    if(!oamDetailVideoIntro){
        console.log(11);
        $(".oam-intro").hide();
    }
};

/*详情页-跳转长视频函数*/
function skipFull() {
   var fullLink = 'http://gaoqing.wx.cibn.cc/vod/detail/'+programCibnId; //跳转长视频
    window.location.href = fullLink;
}

/*video初始化*/
function videoInit(){
    videoPlayer = new createVideo(
        "video_container",	//容器的id
        {
            url 		: oamDetailVideoUrl, 	//视频地址
            autoplay	: true, //是否自动播放
            poster      : oamDetailVideoPoster, //poster
            currentTime : 0 //当前时间
        }
    );
};
/*********************************video部分相关函数结束************************/


/********************************* 精彩导视部分相关函数开始 *********************************/
/*请求精彩导视data*/
function getOAMRecommendListAjax() {
    
    //若在长视频详情页调用精彩导师，需设置oamkeywords字段，传入关键词多个用逗号隔开，例如：oamkeywords = '动作,电影,武侠'
    
    var data = {
        keywords: oamkeywords,  //传入关键词，获取精彩导视
    }
    var basePar = initPar();
    $.extend(data, basePar);
    $.ajax({
        type: 'GET',
        url: website + 'wxcibn/getShortVideoRecommend',
        async: true,   //注意！此处必须是异步的，否则阻塞DOM操作
        dataType: 'json',
        data: data,
        success: function (data) {
            var curPageData = data.data;
            console.log(curPageData);
            setOAMListData(curPageData);
            $(".oam-recommend").show();
        },
    });
}
/*填充精彩导视html*/
function setOAMListData(pageData){
    var j = 0;
    for(var i=0;i<pageData.length;i++){
        var hotImg = formatFileUrlImgCut(pageData[i].contentImg,pageData[i].contentImg,1,2);
        var hotTitle = pageData[i].title;
        var link = encodeURI('detail.html?contentId=' + pageData[i].contentId + '&keywords=' +pageData[i].topicName);
        var html= `
            <li onclick="detailSkip(this)" data-href=${link}>
                <img src=${hotImg} alt=""
                     class="oam-rec-poster">
                <p class="oam-rec-text">
                    ${hotTitle}
                </p>
            </li>
            `
        if(pageData[i].contentId === oamContentId){
            console.log(i);
            continue;
        }else{
            $(".oam-rec-list").append(html);
        };
        j++;
        if(j >= 4){
            break;
        }
    }
}
/*精彩导视跳转详情页*/
function detailSkip(obj) {
    var href = '';
    href = $(obj).attr("data-href");
    window.location.href = href
}
/********************************* 精彩导视部分相关函数结束 *********************************/

